var shopareainfoManager;
var shopareainfoItemPageOpen;
var shopareainfoItem;

function initShopAreaInfoManager(){
	var colimnsArray = [];
	
		colimnsArray.push({
			display : '社区',
			name : 'areaId',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){
				if(item.areaInfo){
					return item.areaInfo.name;
				}
			}
		});
		colimnsArray.push({
			display : '商户',
			name : 'shopId',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){
				if(item.shopInfo){
					return item.shopInfo.name;
				}
			}
		});
		colimnsArray.push({
			display : '商品类型',
			name : 'goodsClassifyId',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){
				if(item.goodsClassifyInfo){
					return item.goodsClassifyInfo.name;
				}
			}
		});
		colimnsArray.push({
			display : '发布上限',
			name : 'num',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
	
	var menu = $.ligerMenu({ width: 120, items:
		[
			{ text: '增加', click: function(){openShopAreaInfoItemPage('create',"新增")}, icon: 'add' },
			{ line: true },
			{ text: '修改', click: function(){openShopAreaInfoItemPage('edit',"编辑")} },
			{ line: true },
			{ text: '查看', click: function(){openShopAreaInfoItemPage('show',"查看")} }
		]
	}); 
	
	return window['g'] = jQuery("#maingrid").ligerGrid({
		height : '100%',
		url:'/admin/shop/shopAreaInfo/ajaxList.do',
		columns : colimnsArray,
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdateShopAreaInfoManager,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : function(){openShopAreaInfoItemPage('create',"新增")},
				icon : 'add'
			}, {
				line : true
			}]
		},
		onContextmenu : function (parm,e)
        {
			shopareainfoItem = parm.data;
	        menu.show({ top: e.pageY, left: e.pageX });
	        return false;
        },
        enabledEdit: false, 
        isScroll: false, 
        rownumbers:true
	});
};

function saveUpdateShopAreaInfoManager(item){
	var data = shopareainfoManager.getUpdated();
	if(data.length == 0){
		$.ligerDialog.warn("您还未做任何修改哦!");
		return;
	}
	doSaveShopAreaInfos(data);
};
function doSaveShopAreaInfos(newData,add){
	jQuery.ajax({
		url:'/admin/shop/shopAreaInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('恭喜,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.sucessList.length+'条记录!' });
				shopareainfoManager.loadServerData({pagesize:10});
				shopareainfoItemPageOpen.hidden();
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
/**
 * 
 * @param item
 * @param target
 */
function openShopAreaInfoItemPage(target,titleName){
	jQuery.ajax({
		url:'/admin/shop/shopAreaInfo/'+target+'.do',
		data:{id:(shopareainfoItem?shopareainfoItem.id:0)},
		dataType:'text',
		type:'post',
		success:function(data){
			if(data){
				jQuery("#openDiv").html(data);
				shopareainfoItemPageOpen = $.ligerDialog.open({ target: $("#openDiv"),isResize:true,title:titleName,width:450 });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};

jQuery(function() {
	shopareainfoManager = initShopAreaInfoManager();
    jQuery("#pageloading").hide();
});